package com.zzy.study.socketANDnio;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.Selector;
import java.nio.channels.SocketChannel;
import java.util.Set;

public class NIOClient {
    /*标识数字*/  
    private static int flag = 0;  
    /*缓冲区大小*/  
    private static int BLOCK = 1024;  
    /*接受数据缓冲区*/  
    private static ByteBuffer sendBuffer = ByteBuffer.allocate(BLOCK);  
    /*发送数据缓冲区*/  
    private static ByteBuffer receiveBuffer = ByteBuffer.allocate(BLOCK);  
    /*服务器端地址*/  
    private final static InetSocketAddress SERVER_ADDRESS = new InetSocketAddress(  
            "192.168.10.130", 9111);  
  
    public static void main(String[] args) throws IOException {  
        SocketChannel clientChannel = SocketChannel.open();  
        clientChannel.configureBlocking(false);  
        Selector selector = Selector.open();  
        clientChannel.register(selector, SelectionKey.OP_CONNECT);  
        clientChannel.connect(SERVER_ADDRESS);  
    
        SocketChannel socketChannel;
        Set<SelectionKey> selectionKeys;    
        String receiveText;  
        String sendText;  
        int count=0;  
  
        while (true) {  
            selector.select();  
            selectionKeys = selector.selectedKeys();  
            for(SelectionKey selectionKey:selectionKeys){ 
                if (selectionKey.isConnectable()) {  
                    System.out.println("client connect");  
                    socketChannel = (SocketChannel) selectionKey.channel();  //
                    if (socketChannel.isConnectionPending()) { 
                        socketChannel.finishConnect();  
                        System.out.println("完成连接!");  
                        sendBuffer.clear();  
                        sendBuffer.put("Hello,Server".getBytes());  
                        sendBuffer.flip();  
                        socketChannel.write(sendBuffer);  
                    }  
                    socketChannel.register(selector, SelectionKey.OP_READ);  
                } else if (selectionKey.isReadable()) {  
                    socketChannel = (SocketChannel) selectionKey.channel();  
                    receiveBuffer.clear();  
                    count=socketChannel.read(receiveBuffer);  
                    if(count>0){  
                        receiveText = new String( receiveBuffer.array(),0,count);  
                        System.out.println("客户端接受服务器端数据--:"+receiveText);  
                        socketChannel.register(selector, SelectionKey.OP_WRITE);  
                    }  
  
                } else if (selectionKey.isWritable()) {  
                    sendBuffer.clear();  
                    socketChannel = (SocketChannel) selectionKey.channel();  
                    sendText = "message from client--" + (flag++);  
                    sendBuffer.put(sendText.getBytes());  
                    sendBuffer.flip();  
                    socketChannel.write(sendBuffer);  
                    System.out.println("客户端向服务器端发送数据--："+sendText);  
                    socketChannel.register(selector, SelectionKey.OP_READ);  
                }  
            }  
            selectionKeys.clear();  
        }  
    }  
}